{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import tensorflow as tf\n",
    "\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "(train_images, train_labels), (test_images, test_labels) = tf.keras.datasets.mnist.load_data()\n",
    "#加载数据集"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "train_images = train_images / 255\n",
    "test_images = test_images/ 255\n",
    "#数据归一化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "train_labels = np.array(pd.get_dummies(train_labels))\n",
    "test_labels = np.array(pd.get_dummies(test_labels))\n",
    "#one-hot编码"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "model = tf.keras.Sequential()\n",
    "#顺序结构的模型搭建"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "model.add(tf.keras.layers.Flatten(input_shape=(28,28)))\n",
    "model.add(tf.keras.layers.Dense(64, activation='sigmoid'))\n",
    "model.add(tf.keras.layers.Dense(10, activation='softmax'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Model: \"sequential\"\n",
      "_________________________________________________________________\n",
      "Layer (type)                 Output Shape              Param #   \n",
      "=================================================================\n",
      "flatten (Flatten)            (None, 784)               0         \n",
      "_________________________________________________________________\n",
      "dense (Dense)                (None, 64)                50240     \n",
      "_________________________________________________________________\n",
      "dense_1 (Dense)              (None, 10)                650       \n",
      "=================================================================\n",
      "Total params: 50,890\n",
      "Trainable params: 50,890\n",
      "Non-trainable params: 0\n",
      "_________________________________________________________________\n"
     ]
    }
   ],
   "source": [
    "model.summary()\n",
    "#模型预览"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "model.compile(optimizer='adam',loss='categorical_crossentropy',metrics=['acc'])\n",
    "#设置优化器，损失函数和记录准确率"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Epoch 1/10\n",
      "1875/1875 [==============================] - 4s 1ms/step - loss: 0.7662 - acc: 0.8198 - val_loss: 0.2554 - val_acc: 0.9260\n",
      "Epoch 2/10\n",
      "1875/1875 [==============================] - 2s 969us/step - loss: 0.2324 - acc: 0.9336 - val_loss: 0.1919 - val_acc: 0.9443\n",
      "Epoch 3/10\n",
      "1875/1875 [==============================] - 2s 978us/step - loss: 0.1782 - acc: 0.9503 - val_loss: 0.1588 - val_acc: 0.9515\n",
      "Epoch 4/10\n",
      "1875/1875 [==============================] - 2s 974us/step - loss: 0.1383 - acc: 0.9613 - val_loss: 0.1379 - val_acc: 0.9592\n",
      "Epoch 5/10\n",
      "1875/1875 [==============================] - 2s 979us/step - loss: 0.1193 - acc: 0.9660 - val_loss: 0.1267 - val_acc: 0.9616\n",
      "Epoch 6/10\n",
      "1875/1875 [==============================] - 2s 983us/step - loss: 0.1070 - acc: 0.9694 - val_loss: 0.1189 - val_acc: 0.9629\n",
      "Epoch 7/10\n",
      "1875/1875 [==============================] - 2s 1ms/step - loss: 0.0885 - acc: 0.9744 - val_loss: 0.1125 - val_acc: 0.9660\n",
      "Epoch 8/10\n",
      "1875/1875 [==============================] - 2s 1ms/step - loss: 0.0797 - acc: 0.9777 - val_loss: 0.1056 - val_acc: 0.9667\n",
      "Epoch 9/10\n",
      "1875/1875 [==============================] - 2s 983us/step - loss: 0.0699 - acc: 0.9800 - val_loss: 0.1019 - val_acc: 0.9676\n",
      "Epoch 10/10\n",
      "1875/1875 [==============================] - 2s 1ms/step - loss: 0.0638 - acc: 0.9820 - val_loss: 0.0996 - val_acc: 0.9682\n"
     ]
    }
   ],
   "source": [
    "history = model.fit(train_images,train_labels,epochs = 10,validation_data=(test_images,test_labels))\n",
    "#载入训练集、验证级、设置训练轮次"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "313/313 [==============================] - 0s 709us/step - loss: 0.0996 - acc: 0.9682\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[0.09960726648569107, 0.9682000279426575]"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model.evaluate(test_images,test_labels)\n",
    "#使用测试集进行评估"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
